import glm
import OpenGL.GL as gl
import numpy as np
import pygl

class SolidWireframe:
    def __init__(self, positions:np.ndarray, normals:np.ndarray, 
                 indices:np.ndarray) -> None:
        gl.glEnable(gl.GL_DEPTH_TEST)
        self.line_width = 0.4
        self.num_vertices = len(positions)
        self.number_indices = len(indices)
        self.program = pygl.ProgramVFG("usecase/shaders/solid_wireframe.vs", 
                                      "usecase/shaders/solid_wireframe.fs",
                                      "usecase/shaders/solid_wireframe.geom")
        self.vao = pygl.VertexArrayObject()
        self.ebo = pygl.ElementBufferObject(indices)
        self.vao.setElementBuffer(self.ebo)
        self.position_vbo = pygl.VertexBufferObject(positions)
        self.normal_vbo = pygl.VertexBufferObject(normals)
        self.vao.setVertexBuffer(self.position_vbo, 0, 0, 
                                 3 * gl.sizeof(gl.GLfloat))
        self.vao.setVertexBuffer(self.normal_vbo, 1, 0, 
                                 3 * gl.sizeof(gl.GLfloat))
        attribute_position = pygl.VertexAttribute("position", 0, 3, 
                                                  gl.GL_FLOAT, False, 0)
        attribute_normal = pygl.VertexAttribute("normal",1, 3, gl.GL_FLOAT, 
                                False, 3 * gl.sizeof(gl.GLfloat))
        self.vao.setVertexAttribute(0, attribute_position)
        self.vao.setVertexAttribute(1, attribute_normal)
        self.rotation = glm.vec3(0.0, 0.0, 0.0)
        self.position = glm.vec3(0.0, 0.0, 0.0)
        self.scale = glm.vec3(1.0, 1.0, 1.0)
        self.updateModelMatrix()
        self.surface_color =  glm.vec3(146.0/255.0, 150.0/255.0, 242.0/255.0)
        self.line_color = glm.vec3(0.3)

    def render(self, view_matrix: glm.mat4, projection_matrix: glm.mat4) -> None:
        self.program.use()
        self.program.setUniformMatrix4fv(0, self.model_matrix)
        self.program.setUniformMatrix4fv(1, view_matrix)
        self.program.setUniformMatrix4fv(2, projection_matrix)
        self.program.setUniform1f(3, self.line_width)
        self.program.setUniform3fv(4, self.surface_color)
        self.program.setUniform3fv(5, self.line_color)
        self.vao.bind()
        gl.glDrawElements(gl.GL_TRIANGLES, self.number_indices, 
                          gl.GL_UNSIGNED_INT, None)

    def delete(self)->None:
        self.vao.delete()
        self.ebo.delete()
        self.position_vbo.delete()
        self.normal_vbo.delete()
        self.program.delete()

    def updateModelMatrix(self)->None:
        scale_matrix = glm.scale(glm.mat4(1.0), self.scale)
        rotation_matrix = glm.rotate(glm.mat4(1.0), glm.radians(self.rotation.x), 
                                     glm.vec3(1.0, 0.0, 0.0))
        rotation_matrix = glm.rotate(rotation_matrix, 
                        glm.radians(self.rotation.y), glm.vec3(0.0, 1.0, 0.0))
        rotation_matrix = glm.rotate(rotation_matrix, 
                        glm.radians(self.rotation.z), glm.vec3(0.0, 0.0, 1.0))
        translation_matrix = glm.translate(glm.mat4(1.0), self.position)
        self.model_matrix = translation_matrix * rotation_matrix * scale_matrix